<template>
  <el-dialog
    class="gl_dialog_TitleLine"
    width="520px"
    :title="title"
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
  >
    <el-form
      ref="api_POST_CompetitionChargeVO"
      v-loading="loading"
      :model="api_POST_CompetitionChargeVO"
      label-width="80px"
      size="mini"
      :rules="api_POST_CompetitionChargeVO_Rules"
      @submit.native.prevent
    >
      <el-row>
        <el-col :span="24">
          <el-form-item label="费用名称" prop="name">
            <span>{{ api_POST_CompetitionChargeVO.name }}</span>
          </el-form-item>
        </el-col>
        <el-col v-if="api_POST_CompetitionChargeVO.chargeTypeCode === 'Entry'" :span="24">
          <el-form-item label="兼项个数" prop="parameter">
            <el-input v-model.trim="api_POST_CompetitionChargeVO.parameter" min="0" max="100" type="number" placeholder="请填写兼项个数" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="价格(元)" prop="amount">
            <el-input v-model.trim="api_POST_CompetitionChargeVO.amount" placeholder="请填写价格" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer">
      <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
      <el-button size="mini" type="primary" @click="valid">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { mapState, mapActions } from 'vuex';
import { CopyData } from '@/lib';
import { Api_POST_CompetitionChargeVO } from '@/lib/form/events';
import { Api_POST_CompetitionChargeVO_Rules } from '@/lib/validate/events';
import {
  api_POST_EventsManagementCompetitionCharge,
  api_PUT_EventsManagementCompetitionChargeId,
  api_GET_EventsManagementCompetitionChargeId
} from '@/extend/api/gameswaggerApi/app';
export default {
  name: 'EditExpenses',
  data() {
    return {
      loading: false,
      title: '新增费用',
      api_POST_CompetitionChargeVO: new Api_POST_CompetitionChargeVO(),
      api_POST_CompetitionChargeVO_Rules: new Api_POST_CompetitionChargeVO_Rules(),
      api_POST_CompetitionChargeDTO: {},
      dialogVisible: false,
      gameId: '',
      // 费用id
      expensesId: ''
    };
  },
  computed: {
    ...mapState({
      dict: state => state.dict.dict
    })
  },
  created() {
    this.gameId = this.$route.params.parentId;
    this.getDict();
  },
  methods: {
    ...mapActions({ getDict: 'dict/getDict' }),
    open(row) {
      this.title = '新增费用';
      this.api_POST_CompetitionChargeVO = new Api_POST_CompetitionChargeVO();
      if (row) {
        this.expensesId = row.id;
        this.title = '编辑费用';
        this.getExpensesById().then(() => {
          this.dialogVisible = true;
        });
      } else {
        this.dialogVisible = true;
      }
      this.$nextTick(() => {
        this.$refs.api_POST_CompetitionChargeVO && this.$refs.api_POST_CompetitionChargeVO.resetFields();
      });
    },
    // 费用类型改变
    changeChargeType(chargeTypeCode) {
      console.log(chargeTypeCode);
      switch (chargeTypeCode) {
        default:
          this.api_POST_CompetitionChargeVO.parameter = '';
          this.api_POST_CompetitionChargeVO.canDelete = true;
          break;
      }
      // 自定义费用不处理
      if (chargeTypeCode === 'Custom' || this.expensesId) return;
      for (let i = 0; i < this.dict.competitionChargeType.length; i++) {
        const item = this.dict.competitionChargeType[i];
        if (item.code === chargeTypeCode) {
          this.api_POST_CompetitionChargeVO.name = item.value;
          break;
        }
      }
    },
    valid() {
      this.$refs.api_POST_CompetitionChargeVO.validate(valid => {
        if (!valid) return;
        this.api_POST_CompetitionChargeVO.competitionId = this.gameId;
        const _api_POST_CompetitionChargeVO = JSON.parse(JSON.stringify(this.api_POST_CompetitionChargeVO));
        _api_POST_CompetitionChargeVO.amount = Number(_api_POST_CompetitionChargeVO.amount) * 100;
        if (this.expensesId) {
          this.editExpenses(_api_POST_CompetitionChargeVO);
        } else {
          this.addExpenses(_api_POST_CompetitionChargeVO);
        }
      });
    },
    // 获取指定赛事费用
    getExpensesById() {
      return new Promise((resolve, reject) => {
        api_GET_EventsManagementCompetitionChargeId(this.expensesId).then(res => {
          this.api_POST_CompetitionChargeDTO = res || {};
          CopyData(this.api_POST_CompetitionChargeDTO, this.api_POST_CompetitionChargeVO);
          this.api_POST_CompetitionChargeVO.amount = this.api_POST_CompetitionChargeVO.amount && (this.api_POST_CompetitionChargeVO.amount / 100 + '');
          resolve(res);
        }, error => {
          reject(error);
          this.$message({ type: 'error', message: '获取赛事费用信息失败！' });
        });
      });
    },
    // 新增赛事费用
    addExpenses(api_POST_CompetitionChargeVO) {
      return new Promise((resolve, reject) => {
        this.loading = true;
        api_POST_EventsManagementCompetitionCharge(api_POST_CompetitionChargeVO).then(res => {
          resolve(res);
          this.$message({ type: 'success', message: '新增赛事费用成功！' });
          this.$emit('refreshPage');
          this.$refs.api_POST_CompetitionChargeVO.resetFields();
          this.loading = false;
          this.dialogVisible = false;
        }, error => {
          reject(error);
          this.$message({ type: 'error', message: '新增赛事费用失败！' + error.error.message });
          this.loading = false;
        });
      });
    },
    // 编辑赛事费用
    editExpenses(api_POST_CompetitionChargeVO) {
      return new Promise((resolve, reject) => {
        this.loading = true;
        api_PUT_EventsManagementCompetitionChargeId(this.expensesId, api_POST_CompetitionChargeVO).then(res => {
          resolve(res);
          this.$message({ type: 'success', message: '编辑赛事费用成功！' });
          this.$emit('refreshPage');
          this.loading = false;
          this.dialogVisible = false;
        }, error => {
          reject(error);
          this.$message({ type: 'error', message: '编辑赛事费用失败！' + error.error.message });
          this.loading = false;
        });
      });
    }
  }
};
</script>
